package configuration.options;


import informationExtractionSystem.InformationExtractor;

import java.util.ArrayList;

import configuration.parameters.DoubleValuedParameter;
import configuration.parameters.Parameter;

import statistics.MeasuredPrecision;
import database.mediator.MediatorInterface;
import distributedRE.model.DistributedREModel;
import distributedRE.model.QueryDatabase;
import distributedRE.model.Session;

public class PrecisionFitter extends AdjustmentOption{

	private MeasuredPrecision mP;
	private double smoothvalue;
	
	public PrecisionFitter(InformationExtractor ie, int K, double smoothvalue){
		
		super(2,K);
		
		this.smoothvalue = smoothvalue;
		
	}
	
	public void adapt(DistributedREModel dREModel, Session s, MediatorInterface mI) {
		
		mP = new MeasuredPrecision(dREModel.getInformationExtractor(),dREModel.getRelation());
		
		ArrayList<QueryDatabase> qDbs = s.getQueryDatabases();
		
		double precision, newprecision;
		
		for (QueryDatabase queryDatabase : qDbs) {
			
			Parameter precisionValue = queryDatabase.getParameter(Parameter.PRECISION);
			
			precision = mP.getQueryDatabaseStatistic(queryDatabase).doubleValue();
			
			newprecision = smoothvalue*precision + (1-smoothvalue)*((Double)precisionValue.getValue()).doubleValue();
			
			queryDatabase.addParameter(Parameter.PRECISION, new DoubleValuedParameter(newprecision));
			
		}
		
	}

	public String toString(){
		return "Precision Fitter with smoothValue " + smoothvalue;
	}

}
